home *** CD-ROM | disk | FTP | other *** search
- /*
-
- StripCode 1.1 (for MathScript 3.1)
-
- Remove formatting code from around the part
- of the formula under the cursor.
-
- e.g. (where | represents the cursor)
-
- (a+b|)+c -> a+b+c
- (a+|(b+c)) -> a+(b+c)
- (a+(|b+c)) -> (a+b+c)
-
-
- Removes the following codes:
-
- ^<obj>
- _<obj>
- \ul<obj>
- \ol<obj>
- \l<b><obj>
- <obj>\r<b>
- \sqrt<obj>
-
- \rt<obj1><obj2> Note: Will only remove the <obj1> part, and only if
- the cursor is within the <obj1> part. The code
- will then be changed to <obj1>\sqrt<obj2>.
-
- \dt<n><obj>
- \not<obj>
- \atilde<obj>
- \aacute<obj>
- \agrave<obj>
- \ahat<obj>
- \abreve<obj>
- \acheck<obj>
- \lvec<obj>
- \rvec<obj>
- \bvec<obj>
-
- <obj>\up\sy<x> Note: Will only work correctly if x is a single character.
- For codes inserted via the GUI, this will always
- be true (single dot, double dot etc.)
-
-
- Version History:
-
- 0.1ß 10.08.96 Proof of concept
- 0.2ß 11.08.96 More codes detected. Drag selection now allowable
- 0.3ß 15.08.96 Workaround for Null Pointer problem
- 0.4ß 16.08.96 Removed unnecessary SetCode/GetCode pair
- 0.5ß 28.08.96 Workaround for NewLine problem. Removed false detection
- of _<obj> when the ^_<obj> code was used. Added a partial
- solution for removal of \rt code.
- 0.6ß 19.09.96 Workarounds for Null Pointer and NewLine removed thanks to
- bugfixes in MathScript beta 4
- 0.7ß 12.10.96 More codes added.
- 1.0 27.10.96 First Public Release
- 1.1 02.02.97 Second Public Release
- Added new codes from MathScript 3.1
-
-
- Chris Coulson (c.j.coulson@ncl.ac.uk)
-
- */
-
- options results
-
- GetCode
- fml = result
- cp = CursorPos(fml)
- fml = StripCode(fml,cp)
- SetCode '"'fml'"'
- EXIT
-
-
-
- /*
- CursorPos
-
- Since there isn't a command to do this within
- Mathscript, we'll emulate it by writing a ! into
- the formula at the cursor position, using InsertCode,
- and then we'll use the ARexx Compare() function to
- find where the ! was inserted. Note that this
- method allows for drag-selection of the formula
- section to be stripped, unlike the earlier and less
- efficient method. Two advantages for the price of one!
- */
-
- CursorPos: PROCEDURE
- PARSE ARG OrgFml
- InsertCode '!'
- GetCode
- CPFml = result
- pos = COMPARE(OrgFml,CPFml)
- RETURN pos
-
-
- /*
- StripCode
-
- Scan left and right from the cursor pos looking for
- the format code locations, then remove them.
- */
-
- StripCode: PROCEDURE
- PARSE ARG fml, cp
- lcp = cp-1
- rcp = cp
- fml_len = LENGTH(fml)
- leave=0
- found=0
- lnest=0
- rnest=0
-
- /*
- Look for the [ and ] characters by moving left and right from
- the starting cursor pos. If a ] is found whilst moving left,
- or a [ is found whilst moving right, this indicates there is
- another formula code within the one we want to remove, and we
- need to bear this in mind when deciding whether any [ or ]
- found belong to the code we want to remove or not. The lnest
- and rnest variables take care of this. Each is decremented
- when a [ or ] is found, and incremented when a ] or [ is found.
- When one reaches -1, further scanning in that direction ceases.
- When both reach -1, we know we have found the correct [ and ]
- for our code.
- */
-
- DO UNTIL leave=1
- IF (lcp > 0)&(lnest > -1) THEN
- DO
- lchar = SUBSTR(fml,lcp,1)
- IF lchar = ']' THEN lnest = lnest + 1
- IF lchar = '[' THEN
- DO
- lnest = lnest - 1
- IF lnest = -1 THEN lpos = lcp
- END
- lcp = lcp - 1
- END
- IF (rcp < fml_len+1)&(rnest > -1) THEN
- DO
- rchar = SUBSTR(fml,rcp,1)
- IF rchar = '[' THEN rnest = rnest + 1
- IF rchar = ']' THEN
- DO
- rnest = rnest - 1
- IF rnest = -1 THEN rpos = rcp
- END
- rcp = rcp + 1
- END
-
- /*
- If we have both lnest and rnest =-1, we have found a valid [] pair,
- and need to check it out further, so we set the found flag. If,
- on the other hand, we have reached both ends of the formula string
- without finding a [] pair, we just exit without setting the found flag.
- */
-
- IF (lnest=-1)&(rnest=-1) THEN
- DO
- leave=1
- found=1
- END
- IF (lcp=0)&(rcp=fml_len+1) THEN leave=1
- END
-
- /*
- We've found a control code, so strip it out!
- */
-
- IF found=1 THEN
- DO
- strip_left=0
- strip_right=0
- lc1=''
- lc2.=''
- lc3.=''
- lc5=''
- rc2=''
- rc3.=''
- rc4=''
-
- /*
- Grab a series of substrings from the formula string, to be checked
- against the codes the program can safely remove.
- */
-
- IF lpos > 1 THEN lc1=SUBSTR(fml,lpos-1,1,' ')
- IF lpos > 2 THEN lc2.1=SUBSTR(fml,lpos-2,2,' ')
- IF lpos > 3 THEN lc2.2=SUBSTR(fml,lpos-3,2,' ')
- IF lpos > 3 THEN lc3.1=SUBSTR(fml,lpos-3,3,' ')
- IF lpos > 4 THEN lc3.2=SUBSTR(fml,lpos-4,3,' ')
- IF lpos > 4 THEN lc4=SUBSTR(fml,lpos-4,4,' ')
- IF lpos > 5 THEN lc5=SUBSTR(fml,lpos-5,5,' ')
- IF lpos > 7 THEN lc7=SUBSTR(fml,lpos-7,7,' ')
- IF rpos < fml_len-1 THEN rc2=SUBSTR(fml,rpos+1,2,' ')
- IF rpos < fml_len-2 THEN rc3.1=SUBSTR(fml,rpos+1,3,' ')
- IF rpos < fml_len-6 THEN rc3.2=SUBSTR(fml,rpos+4,3,' ')
- IF rpos < fml_len-6 THEN rc4=SUBSTR(fml,rpos+4,4,' ')
-
- /*
- Compare the grabbed strings to the removable formula codes
- */
-
- IF (lc1 = '^')|(lc1 = '_')&~(lc2.1 = '^_') THEN strip_left=1
- IF (lc3.1 = '\ul')|(lc3.1 = '\ol')|(lc2.2 = '\l') THEN strip_left=3
- IF lc3.2 = '\dt' THEN strip_left=4
- IF lc4 = '\not' THEN strip_left=4
- IF (lc5 = '\sqrt')|(lc5 = '\ahat')|(lc5 = '\lvec')|(lc5 = '\rvec')|(lc5 = '\bvec') THEN strip_left=5
- IF (lc7 = '\atilde')|(lc7 = '\aacute')|(lc7 = '\agrave')|(lc7 = '\abreve')|(lc7 = '\acheck') THEN strip_left=7
-
- IF rc2 = '\r' THEN strip_right=3
-
- /*
- For the \up code, both \up[\sy<x>] and \up\sy<x> are valid. The first
- form is found in pre MS3 formulae, whereas the second is the form
- inserted by MS3
- */
-
- IF (rc3.1 = '\up')&(rc3.2 = '\sy') THEN strip_right=7
- IF (rc3.1 = '\up')&(rc4 = '[\sy') THEN strip_right=9
-
- /*
- If we have found a removable code, then remove it...
- */
-
- IF (strip_left > 0)|(strip_right > 0) THEN
- DO
- lleft = lpos-strip_left-1
- lcentre = rpos-lpos-1
- pcentre = lpos+1
- lright = fml_len-rpos-strip_right
- fml = LEFT(fml,lleft)||SUBSTR(fml,pcentre,lcentre)||RIGHT(fml,lright)
- END
-
- /*
- Deal with the special case of \rt[][]
- */
-
- IF lc3.1 = '\rt' THEN
- DO
- lleft = lpos-4
- lcentre = rpos-lpos-1
- pcentre = lpos+1
- lright = fml_len-rpos
- fml = LEFT(fml,lleft)||SUBSTR(fml,pcentre,lcentre)||RIGHT(fml,lright)
- fml = INSERT('\sqrt',fml,lleft+1)
- END
- END
- RETURN fml
-